﻿static void Main(string[] args)
{
    GenericList<int> intlist = new GenericList<int>();
    for (int i = 0; i < 10; i++)
    {
        intlist.Add(i);
    }

    // 打印链表元素
    intlist.ForEach(x => Console.WriteLine(x));

    // 求最大值最小值
    int max = int.MinValue;
    intlist.ForEach(x => max = Math.Max(max, x));
    Console.WriteLine($"最大值: {max}");
    int min = int.MaxValue;
    intlist.ForEach(x => min = Math.Min(min, x));
    Console.WriteLine($"最小值: {min}");

    // 求和
    int sum = 0;
    intlist.ForEach(x => sum += x);
    Console.WriteLine($"求和: {sum}");

    GenericList<string> strlist = new GenericList<string>();
    for (int x = 0; x < 10; x++)
    {
        strlist.Add("str" + x);
    }

    // 打印字符串链表元素
    Console.WriteLine("字符串链表元素:");
    strlist.ForEach(x => Console.WriteLine(x));
    


}

public class Node<T>
{
    public Node<T> Next { get; set; }
    public T Data { get; set; }

    public Node(T t)
    {
        Next = null;
        Data = t;
    }
}

public class GenericList<T>
{
    public Node<T> tail;
    public Node<T> head;

    public GenericList()
    {
        tail = head = null;
    }

    public Node<T> Head
    {
        get => head;
    }

    public void Add(T t)
    {
        Node<T> n = new Node<T>(t);
        if (tail == null)
        {
            head = tail = n;
        }
        else
        {
            tail.Next = n;
            tail = n;
        }
    }

    // ForEach 
    public void ForEach(Action<T> action)
    {
        for (Node<T> node = head; node != null; node = node.Next)
        {
            action(node.Data);
        }
    }
}